//构造函数
function City(obj){
    Object.assign(this,obj)
    this.init()
}

//原型
City.prototype ={
    constructor:City,
    init:function(){
        this.render()
        this.move();
    },

    render:function(){
        //渲染省
        this.renderFn(this.sheng,this.data)
          //渲染市
        this.renderFn(this.shi,this.data[0].children)
        //区
        this.renderFn(this.qu,this.data[0].children[0].children)
        // 街道
        this.renderFn(this.jiedao,this.data[0].children[0].children[0].children)
    },
    //封装方法渲染方法
    renderFn:function(dom,data){
        dom.innerHTML = data.map((item,index)=>{
            return `<li>${item.name}</li>`
        }).join('')
    },
    move:function(){
        var shengIndex=0
        var shiIndex=0
        var quIndex=0
        this.sheng.addEventListener('click',(e)=>{
            var el=e.target;
            if(el.nodeName=='LI'){
                shengIndex=[...this.sheng.children].indexOf(el);
                this.renderFn(this.shi,this.data[shengIndex].children)
                //区
                this.renderFn(this.qu,this.data[shengIndex].children[0].children)
                // 街道
                this.renderFn(this.jiedao,this.data[shengIndex].children[0].children[0].children)
            }
        })
        this.shi.addEventListener('click',(e)=>{
            var el=e.target;
            if(el.nodeName=='LI'){
                shiIndex=[...this.shi.children].indexOf(el);
                //区
                this.renderFn(this.qu,this.data[shengIndex].children[shiIndex].children)
                // 街道
                this.renderFn(this.jiedao,this.data[shengIndex].children[shiIndex].children[0].children)
            }
        })
        this.qu.addEventListener('click',(e)=>{
            var el=e.target;
            if(el.nodeName=='LI'){
                quIndex=[...this.qu.children].indexOf(el);
                // 街道
                this.renderFn(this.jiedao,this.data[shengIndex].children[shiIndex].children[quIndex].children)
            }
        })
        this.jiedao.addEventListener('click',(e)=>{
            var el=e.target;
            if(el.nodeName=='LI'){
                shengIndex=[...this.jiedao.children].indexOf(el);
            }
        })
    }
}


